<!--
 * @Author: C.
 * @Date: 2023-03-07 11:18:13
-->
<template>
  <BasicModal v-bind="$attrs" @register="registerModal" @ok="handleSubmit">
    <BasicTable @register="register" />
  </BasicModal>
</template>
<script lang="ts" setup name="TableModel">
  import { ref } from 'vue';
  import { BasicTable, useTable, BasicTableProps } from '/@/components/Table';
  import { BasicModal, useModalInner } from '/@/components/Modal';

  const emit = defineEmits(['success', 'register']);

  const [registerTable, tableActions] = useTable();
  const initProps = ref({
    useSearchForm: true,
    showTableSetting: false,
    resizeHeightOffset: 300,
    canResize: true,
    showIndexColumn: false,
    rowSelection: {
      type: 'checkbox',
    },
    formConfig: {
      labelWidth: 120,
      schemas: [],
    },
    columns: [],
  });
  const [registerModal, modelActions] = useModalInner((data) => {
    if (data?.init) {
      Object.assign(initProps.value, data);
    }

    modelActions.setModalProps({ confirmLoading: false });
  });
  function register(instance, formInstance) {
    registerTable(instance, formInstance);
    initProps.value && tableActions.setProps(initProps.value as BasicTableProps);
  }
  async function handleSubmit() {
    try {
      modelActions.setModalProps({ confirmLoading: true });
      const selects = tableActions.getSelectRows();
      emit('success', selects);
      tableActions.clearSelectedRowKeys();
      modelActions.closeModal();
    } finally {
      modelActions.setModalProps({ confirmLoading: false });
    }
  }
</script>
